package com.bridgeintelligent.tag.remotefile.freedim;

import com.bridgeintelligent.tag.remotefile.RemoteFileHandler;
import com.bridgeintelligent.tag.remotefile.fastdfs.FastDFSException;
import com.wayneleo.quickstart.framework.IDGenerator;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by JackCheung on 2019/9/19 3:36 PM.
 */
@AllArgsConstructor
@Component
public class DimFreeFileUtils implements IDimFreeFileUtils {

    private RemoteFileHandler handler;

    /**
     * 保存标签文件
     *
     * @param multipartFile
     * @param dimId
     * @return
     */
    @Override
    public String save(MultipartFile multipartFile, String dimId) {
        String path = "";
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        PrintWriter out = null;
        try {
            inputStreamReader = new InputStreamReader(multipartFile.getInputStream(), "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String read;
            String localPath = String.format(FORMAT, dimId);
            out = new PrintWriter(new BufferedWriter(new FileWriter(localPath, true)));
            while ((read = bufferedReader.readLine()) != null) {
                if (read.length() > 0) {
                    out.println(read.trim());
                }
            }
            out.flush();
            path = handler.uploadWhiteList(localPath, dimId);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (out != null) {
                out.close();
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return path;
    }

    /**
     * 保存新增文件
     *
     * @param multipartFile
     * @param dimId
     * @return
     */
    @Override
    public String saveAddFile(MultipartFile multipartFile, String dimId) {
        return save(multipartFile, String.format(ADD_FORMAT, dimId));
    }

    /**
     * 保存删除文件
     *
     * @param multipartFile
     * @param dimId
     * @return
     */
    @Override
    public String saveDelFile(MultipartFile multipartFile, String dimId) {
        return save(multipartFile, String.format(DEL_FORMAT, dimId));
    }

    /**
     * 新增文件
     *
     * @param addFilePath
     * @param filePath
     * @param dimId
     */
    @Override
    public void add(String addFilePath, String filePath, String dimId) {
        FileInputStream fileInputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        PrintWriter out = null;
        try {
            fileInputStream = new FileInputStream(new File(addFilePath));
            inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String read;
            out = new PrintWriter(new BufferedWriter(new FileWriter(filePath, true)));
            while ((read = bufferedReader.readLine()) != null) {
                out.println(read.trim());
            }
            out.flush();
            handler.delete(addFilePath);
        } catch (IOException e) {
            e.printStackTrace();
            throw new FastDFSException(10002, e.getMessage());
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                out.close();
            }
        }
    }

    /**
     * 删除文件
     *
     * @param delFilePath
     * @param filePath
     * @param dimId
     * @return
     */
    @Override
    public String del(String delFilePath, String filePath, String dimId) {
        Map<String, String> map = getSaveMap(delFilePath, filePath);
        String localPath = String.format(FORMAT, IDGenerator.gen());
        try {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(localPath, true)));
            map.forEach((k, v) -> out.println(k));
            out.close();
            handler.uploadWhiteList(localPath, dimId);
            handler.delete(delFilePath);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 备份文件
     *
     * @param delFilePath
     * @param filePath
     * @return
     */
    private Map<String, String> getSaveMap(String delFilePath, String filePath) {
        Map<String, String> map = new HashMap<>();
        List<String> list = new ArrayList<>();
        FileInputStream fileInputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        FileInputStream fileInputStreamAll = null;
        InputStreamReader inputStreamReaderAll = null;
        BufferedReader bufferedReaderAll = null;
        try {
            fileInputStream = new FileInputStream(new File(delFilePath));
            inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
            bufferedReader = new BufferedReader(inputStreamReader);
            String read;
            while ((read = bufferedReader.readLine()) != null) {
                list.add(read.trim());
            }
            fileInputStreamAll = new FileInputStream(new File(filePath));
            inputStreamReaderAll = new InputStreamReader(fileInputStreamAll, "UTF-8");
            bufferedReaderAll = new BufferedReader(inputStreamReaderAll);
            String readAll;
            while ((readAll = bufferedReaderAll.readLine()) != null) {
                map.put(readAll.trim(), "");
            }
            list.forEach(map::remove);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fileInputStream != null) {
                try {
                    fileInputStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStreamReader != null) {
                try {
                    inputStreamReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReader != null) {
                try {
                    bufferedReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fileInputStreamAll != null) {
                try {
                    fileInputStreamAll.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (inputStreamReaderAll != null) {
                try {
                    inputStreamReaderAll.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (bufferedReaderAll != null) {
                try {
                    bufferedReaderAll.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return map;
    }
}
